package game;

import javax.swing.JPanel;

/** Class to restrict movement of players, enemies, and bullets */
public class Boundary {

    public static final int ENTIRE_SCREEN = 0;
    public static final int TOP_HALF = 1;
    public static final int BOTTOM_HALF = 2;

    public int getMinX() {
	return minX;
    }

    public void setMinX(int minX) {
	this.minX = minX;
    }

    public int getMinY() {
	return minY;
    }

    public void setMinY(int minY) {
	this.minY = minY;
    }

    public int getMaxX() {
	return maxX;
    }

    public void setMaxX(int maxX) {
	this.maxX = maxX;
    }

    public int getMaxY() {
	return maxY;
    }

    public void setMaxY(int maxY) {
	this.maxY = maxY;
    }

    private int minX;
    private int minY;
    private int maxX;
    private int maxY;

    /**
     * Constructs a boundary within a set of coordinates
     * @param minX minimum x
     * @param minY minimum y
     * @param maxX maximum x
     * @param maxY maximum y
     */
    public Boundary(int minX, int minY, int maxX, int maxY) {
	this.minX = minX;
	this.minY = minY;
	this.maxX = maxX;
	this.maxY = maxY;
    }

    /**
     * Constructs a boundary from a provided JPanel
     * @param screen The screen to calculate restrictions from
     * @param preset ENTIRE_SCREEN, TOP_HALF, or BOTTOM_HALF
     */
    public Boundary(JPanel screen, int preset) {
	if (preset == ENTIRE_SCREEN) {
	    minX = 0; 
	    minY = 0;
	    maxX = screen.getSize().width;
	    maxY = screen.getSize().height;
	} else if (preset == TOP_HALF) {
	    minX = 0;
	    minY = 0;
	    maxX = screen.getSize().width;
	    maxY = screen.getSize().height / 2;
	} else if (preset == BOTTOM_HALF) {
	    minX = 0;
	    minY = screen.getSize().height / 2;
	    maxX = screen.getSize().width;
	    maxY = screen.getSize().height;
	}

    }

    /** Determines if an entity is outside of prescribed bounds */
    public boolean isOutOfBounds(int x, int y) {
	if (x < minX || x > maxX || y < minY || y > maxY) {
	    return true;
	} else {
	    return false;
	}
    }

    /** Determines which way to bounce in */
    public double getBounceAngle(boolean canMoveX, boolean canMoveY, double angle) {
	int quadrent = 0;
	double finalQuadrent = 0;
	while (angle >= 0) {
	    angle -= Math.PI/2;
	    quadrent++;
	}
	switch(quadrent) {
	case 1: {
	    if (canMoveX) {
		finalQuadrent = 4;
	    } else {
		finalQuadrent = 2;
		if (!canMoveY) {
		    finalQuadrent = 3; //bouncing in a corner, very unlikely
		}
	    }
	    break;
	}
	case 2: {
	    if (canMoveX) {
		finalQuadrent = 3;
	    } else {
		finalQuadrent = 1;
		if (!canMoveY) {
		    finalQuadrent = 4;
		}
	    }
	    break;
	}
	case 3: {
	    if (canMoveX) {
		finalQuadrent = 2;
	    } else {
		finalQuadrent = 4;
		if (!canMoveY) {
		    finalQuadrent = 1;
		}
	    }
	    break;
	}
	case 4: {
	    if (canMoveX) {
		finalQuadrent = 1;
	    } else {
		finalQuadrent = 3;
		if (!canMoveY) {
		    finalQuadrent = 2;
		}
	    }
	    break;
	}
	}
	if (finalQuadrent == 0) {
	    finalQuadrent = 3; //weird instance of getting stuck with advanced player AI
	}
	//System.out.println("Quadrent " +  finalQuadrent);
	return Math.PI/2 * (finalQuadrent - 1) + Math.random() * Math.PI/2;

    }


}
